﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using TSV.Areas.Administration.Models;
using TSV.Models.Entities;
using TSV.Models;
using TSV.Library.Common;
using TSV.Library.Attributes;

namespace TSV.Areas.Administration.Controllers
{
    public class PromotionController : BaseController
    {
        private PromotionDao promotionDao = new PromotionDao();
        //
        // GET: /Administration/Promotion/

        [CrmAuthorizeAttribute(Module = Modules.QL_Tin_Khuyen_Mai, Rights = Permissions.Doc)]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// Get and bind to grid
        /// </summary>
        /// <param name="optionSearch"></param>
        /// <returns></returns>
        [CrmAuthorizeAttribute(Module = Modules.QL_Tin_Khuyen_Mai, Rights = Permissions.Doc)]
        public ActionResult GetListPromotionJQGrid()
        {
            //grid constant => duy hung            
            #region JQGrid Params

            string sortColumn = (Request.Params[GridConstants.SORT_COLUMN]).ToString();
            string sortOrder = (Request.Params[GridConstants.SORT_ORDER]).ToString();
            int pageIndex = Convert.ToInt32(Request.Params[GridConstants.PAGE_INDEX]);
            int rowCount = Convert.ToInt32(Request.Params[GridConstants.ROW_COUNT]);

            #endregion

            List<Promotion> list = promotionDao.GetList();

            int totalRecords = list.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)rowCount);
            int currentPage = CommonFunc.SetJqGridPageIndex(totalRecords, pageIndex, rowCount);
            var finalList = promotionDao.Sort(list, sortColumn, sortOrder)
                                  .Skip((currentPage - 1) * rowCount)
                                   .Take(rowCount);
            var jsonData = new
            {
                total = totalPages,
                page = currentPage,
                records = totalRecords,
                rows = (
                    from m in finalList
                    select new
                    {
                        i = m.ID,
                        cell = new string[] {
                            m.ID.ToString(),
                            "<img alt=\"Hình\" width=\"100px\" height=\"100px\" src=\"/FileUpload/Images/Partner/" + m.Image + "\"/>",                            
                            HttpUtility.HtmlEncode(m.Title),   
                            "<input type='button' name='btnMoveUp' class='icon moveup' title='Move up' onclick=\"JS_LIB.redirect('/Administration/Promotion/MoveUp/" + m.ID.ToString() + "')\" />"
                            +"<input type='button' name='btnMoveDown' class='icon movedown' title='Move down' onclick=\"JS_LIB.redirect('/Administration/Promotion/MoveDown/" + m.ID.ToString() + "')\"/>",
                            "<input type=\"button\" class=\"icon edit\" title=\"Cập nhật\" onclick=\"JS_LIB.redirect('/Administration/Promotion/Edit/" + m.ID.ToString() + "')\" />"
                        }
                    }
                ).ToArray()
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        [CrmAuthorizeAttribute(Module = Modules.QL_Tin_Khuyen_Mai, Rights = Permissions.Sua)]
        public ActionResult Edit(int id)
        {
            Promotion promotion = promotionDao.GetById(id);
            return View(promotion);
        }

        //
        // GET: /Administration/Partner/Promotion/5
        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        [CrmAuthorizeAttribute(Module = Modules.QL_Tin_Khuyen_Mai, Rights = Permissions.Sua)]
        public ActionResult Edit(Promotion promotion, FormCollection collection)
        {
            promotion.ID = int.Parse(collection["PromotionID"]);
            if (string.IsNullOrEmpty(collection["Photograph"]))
            {
                promotion.Image = "NoImage.jpg";
            }
            else
            {
                promotion.Image = collection["Photograph"];
            }
            // TODO: Add update logic here
            Message msg = promotionDao.Update(promotion);
            ShowMessage(msg);
            return RedirectToAction("Index");
        }

        [CrmAuthorizeAttribute(Module = Modules.QL_Tin_Khuyen_Mai, Rights = Permissions.Sua)]
        public ActionResult MoveUp(int id)
        {
            Promotion promotion = promotionDao.GetById(id);
            Message msg = promotionDao.MoveUp(promotion);
            return RedirectToAction("Index");
        }

        [CrmAuthorizeAttribute(Module = Modules.QL_Tin_Khuyen_Mai, Rights = Permissions.Sua)]
        public ActionResult MoveDown(int id)
        {
            Promotion promotion = promotionDao.GetById(id);
            Message msg = promotionDao.MoveDown(promotion);
            return RedirectToAction("Index");
        }
    }
}
